package com.awesome.todo.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.awesome.todo.R;
import com.awesome.todo.User;
import com.awesome.todo.database.UserDataSource;

/**
 * This class displays the login screen. It gives the user the option to either
 * sign up or sign in to the application.
 * 
 * @author Doug Black
 * 
 */

public class LoginActivity extends Activity {

	public static Button signUpButton;
	public static Button loginButton;
	public static EditText loginEditText;
	public static EditText passwordEditText;
	public static User currentUser;

	@Override
	/**
	 * This method is called when the Activity is first created. As basic setup, it takes the objects from 
	 * the login.xml file and links them to Java variables that we can then manipulate programatically 
	 * It also creates listener classes and methods for each of the buttons created 
	 * in this method.
	 
	 * In essence, it instantiates the UI.
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		final UserDataSource userData = new UserDataSource(this.getApplicationContext());

		signUpButton = (Button) findViewById(R.id.signUpButton);
		loginButton = (Button) findViewById(R.id.loginButton);
		loginEditText = (EditText) findViewById(R.id.userLoginEditText);
		passwordEditText = (EditText) findViewById(R.id.passwordEditText);

		loginButton.setOnClickListener(new OnClickListener() {

			/**
			 * This method is called when the loginButton is clicked. It takes
			 * the values that the user inputed (the login and password) and
			 * logs the user in.
			 */
			public void onClick(View v) {
				String login = loginEditText.getText().toString();
				String password = passwordEditText.getText().toString();

				currentUser = userData.tryUserLogin(login, password);

				Context context = getApplicationContext();
				int duration = Toast.LENGTH_SHORT;
				CharSequence text = "Incorrect login credentials";

				if (currentUser != null) {
					text = "Successfully logged in! Email: " + currentUser.getEmail();
					Intent intent = new Intent(v.getContext(), TaskListActivity.class);
					intent.putExtra("userId", currentUser.getId());
					startActivity(intent);

				}

				Toast toast = Toast.makeText(context, text, duration);
				toast.show();

				Log.i("LOGGING IN", "Name: " + login);
				Log.i("LOGGING IN", "Password: " + password);

			}
		});

		signUpButton.setOnClickListener(new OnClickListener() {

			/**
			 * This method opens the NewUser activity that handles creating new
			 * user accounts.
			 */
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), NewUserActivity.class);
				startActivity(intent);
			}

		});

	}
}